package org.footballmanager.pandafootball.system.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.footballmanager.pandafootball.common.constants.Constants;
import org.footballmanager.pandafootball.system.domain.FootDj;
import org.footballmanager.pandafootball.system.domain.FootReward;
import org.footballmanager.pandafootball.system.form.impl.FootRewardSearchForm;
import org.footballmanager.pandafootball.system.mapper.FootRewardMapper;
import org.footballmanager.pandafootball.system.service.FootRewardService;
import org.footballmanager.pandafootball.system.service.FootTaxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import tk.mybatis.mapper.entity.Example;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.List;

@Slf4j
@Service
public class FootRewardServiceImpl extends ServiceImpl<FootReward> implements FootRewardService {
    @Autowired
    private FootRewardMapper footRewardMapper;
    @Autowired
    private FootTaxService footTaxService;
    @Autowired
    private RedisService redisService;

    /**
     * 查询
     *
     * @param entityname          实体类名
     * @param map                 参数
     * @param footRewardSearchForm 条件
     */
    @Override
    public void search(String entityname, Model map, FootRewardSearchForm footRewardSearchForm) {
        map.addAttribute(String.format(Constants.FORMNAME, entityname), footRewardSearchForm);
        Page<FootReward> page = PageHelper.startPage(footRewardSearchForm.getPageIndex(), footRewardSearchForm.getPageSize());
        List<FootReward> list = this.findall(footRewardSearchForm);
        map.addAttribute(Constants.PI, page.toPageInfo());
        map.addAttribute(String.format(Constants.LISTENTITY, entityname), list);
    }

    /**
     * 删除所有
     *
     * @param footRewardSearchForm 条件
     * @param request             请求
     */
    @Override
    public void deleteAll(FootRewardSearchForm footRewardSearchForm, HttpServletRequest request) {
        if (footRewardSearchForm.getSelectIds() != null) {
            String[] result = footRewardSearchForm.getSelectIds().split(Constants.COMMA);
            for (String id : result) {
                this.delete(Long.parseLong(id), request);
            }
        }
    }

    /**
     * 翻页查询
     *
     * @param footRewardSearchForm
     * @return
     */
    @Override
    public List<FootReward> findall(FootRewardSearchForm footRewardSearchForm) {
        return footRewardMapper.findall(footRewardSearchForm);
    }

    /**
     * 根据主键查询记录
     * @param id 抽奖记录主键
     * @return 抽奖记录
     */
    @Override
    public FootReward get(Serializable id) {
        return footRewardMapper.findById(id);
    }

    /**
     * 根据用户查找抽奖信息
     *
     * @param userId 用户主键
     * @return 抽奖信息
     */
    @Override
    public FootReward findByUserId(Long userId) {
        Example example = new Example(FootReward.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", userId);
        criteria.andEqualTo("delFlag", 0);
        return this.findByExample(example);
    }

    /**
     * 保存新的抽奖记录设置
     *
     * @param userId  用户主键
     * @param request 请求
     */
    @Override
    public void saveNewReward(Long userId, HttpServletRequest request) {
        FootReward reward = new FootReward();
        reward.setGetTimes(0);
        reward.setGetTimesFree(0);
        reward.setTotalTimes(0);
        reward.setUserId(userId);
        reward.setNeedPay(footTaxService.getRewardPay());
        reward.setMustGet(footTaxService.getMustGet());
        reward.setOdds(footTaxService.getOdds());
        reward.setShowOdds(footTaxService.getShowOdds());
        reward.setRewardModTimes(0);
        List<FootDj> list = (List<FootDj>) redisService.get("listdj");
        for(FootDj dj: list){
            if(dj.getQydj().equals("顶级球星")){
                reward.setOrange(dj.getInitRatio());
            }else if(dj.getQydj().equals("超级球星")){
                reward.setPurple(dj.getInitRatio());
            }else if(dj.getQydj().equals("洲际球星")){
                reward.setBlue(dj.getInitRatio());
            }else if(dj.getQydj().equals("普通球星")){
                reward.setGreen(dj.getInitRatio());
            }else{
                reward.setWhite(dj.getInitRatio());
            }
        }
        this.save(reward, request);
    }

    /**
     * 根据用户主键及类型更新数据
     *  @param userId 用户主键
     * @param type   类型
     * @param request 请求
     */
    @Override
    public void updateTimesByType(Long userId, int type, HttpServletRequest request) {
        FootReward reward = this.findByUserId(userId);
        if(reward != null){
            reward.setTotalTimes(reward.getTotalTimes() + 1);
            if(type == 0){
                //免费抽奖
                reward.setGetTimesFree(reward.getGetTimesFree() + 1);
            }else{
                //收费抽奖
                reward.setGetTimes(reward.getGetTimes() + 1);
            }
            this.updateNotNull(reward, request);
        }
    }

    @Scheduled(cron = "0 0 0 * * ?") //每天0点触发
    @Transactional
    @Override
    public void clearRewardFree() {
        List<FootReward> list = this.getall();
        for(FootReward reward: list){
            reward.setGetTimesFree(0);
            reward.setGetTimes(0);
            this.updateNotNull(reward, null);
        }
    }
}
